#include "Texture.h"

// default constructor setting initial values to 0
Texture::Texture()
{
	// set view rect to default
	m_viewRect.right = m_viewRect.left = m_viewRect.bottom = m_viewRect.top = -1;
	// set pointers to null
	engine = 0;
}

// make sure that safe release is called
Texture::~Texture()
{
	release();
}

void Texture::setTint(int R, int G, int B)
{
	m_R = R;
	m_G = G;
	m_B = B;
}

// setup texture object and load the given file
void Texture::initialize(LPCWSTR fileName, bool relativeToCamera)
{
	// set type to base texture
	m_type = BASIC;

	// save relativity to camera
	m_relativeToCamera = relativeToCamera;

	// set view rect to default
	m_viewRect.right = m_viewRect.left = m_viewRect.bottom = m_viewRect.top = -1;

	// first get an instance of the graphics engine
	if(!engine)
		engine = DX2DEngine::getInstance();

	// set color to white by default
	m_R = m_G = m_B = 255;

	// get the length of fileName
	int i = 0;
	while(fileName[i] != 0)
	{
		i++;
	}
	// increment one more time for the null terminator
	i++;

	// also load from graphics engine
	textureID = engine->createTexture(fileName, i);
}

// safetly release all data
void Texture::release()
{
	//m_rects.release();
}

// draw the texture with the given coordinates
// layer is the Z layer the texture will be drawn at
void Texture::draw(V2DF pos, float rotAngle, float scale)
{
	int layer = 0;
	// make sure layer is less than 1 and greater than -1
	if(layer >= 0.1)
		layer = .9;
	if(layer < 0)
		layer = 0;
	// check if a view rect has been set
	if(m_viewRect.top == -1)
	{
		// pass in view rect
		engine->renderTexture(textureID, pos, layer, scale, rotAngle, 0, m_R, m_G, m_B, m_relativeToCamera);
	}
	else
	{
		// pass in 0 for sprite sheet rect
		engine->renderTexture(textureID, pos, layer, scale, rotAngle, &m_viewRect, m_R, m_G, m_B, m_relativeToCamera);
	}
}

int Texture::imageHeight()
{
	return engine->imageInfo(textureID).Height;
}

int Texture::imageWidth()
{
	return engine->imageInfo(textureID).Width;
}

void Texture::resetViewRect()
{
	m_viewRect.right = m_viewRect.left = m_viewRect.bottom = m_viewRect.top = -1;
}

// if the view rect is larger than the image it will be set to image limits
void Texture::setViewRect(RECT a_viewRect)
{
	// make sure that the rect is limited to the size of the image
	m_viewRect = a_viewRect;
	// restrict left
	if(m_viewRect.left < 0)
		m_viewRect.left = 0;
	if(m_viewRect.left > imageWidth())
		m_viewRect.left = imageWidth();
	// restrict right
	if(m_viewRect.right < 0)
		m_viewRect.right = 0;
	if(m_viewRect.right > imageWidth())
		m_viewRect.right = imageWidth();
	// restrict top
	if(m_viewRect.top < 0)
		m_viewRect.top = 0;
	if(m_viewRect.top > imageHeight())
		m_viewRect.top = imageHeight();
	// restrict bottom
	if(m_viewRect.bottom < 0)
		m_viewRect.bottom = 0;
	if(m_viewRect.bottom > imageHeight())
		m_viewRect.bottom = imageHeight();
}